/*
*    へ　　　　　  ／|
*    /＼7　　   ∠＿/
*    /　│　　 ／　／               
*    │　Z ＿,＜　／　　 /`ヽ     
*    │　　　　　ヽ　　 /　　〉    
*    Y　　　　　  `  /　　/     
*    ｲ ●　､　●　⊂⊃〈　　/       
*    ()  へ　　　　 |　＼       
*    >ｰ ､_　 ィ　 │ ／／        | @author:smily
*    / へ　　 /　ﾉ＜| ＼＼       | @project:net-hole
*    ヽ_ﾉ　　(_／　 │／／        | @file: main.cpp
*    7　　　　　　　|／          | @time: 2020/12/25 16:29
*====＞―r￣￣`ｰ―＿===========    
*^^^^^^^^^^^^^^^^^^^^^^^^^^^
*/
#include "../include/MySocket.h"
#include "../include/NhSocket.h"
#include <iostream>

using namespace std;

int main(int argc, char *argv[]) {
    uint_fast16_t port;
    const char *server_ip, *remote_ip;
    if (argc == 1) {
        port = 6666;
        server_ip = "127.0.0.1";
    } else if (argc == 2) {
        port = 6666;
        server_ip = argv[1];
    } else {
        port = atoi(argv[2]);
        server_ip = argv[1];
    }

    char err_msg[256], buffer[1024];
    int client_socket, recv_len;
    bool b_end = false;
    NhSocket my_socket_client;
    if (!my_socket_client.init_socket()) {
        printf("%s", my_socket_client.get_err_msg());
        return 0;
    }
    if (!my_socket_client.connect_remote(my_socket_client.get_server_addr(), server_ip, port)) {
        printf("%s", my_socket_client.get_err_msg());
        NhSocket::close_socket(my_socket_client.get_local_fd());
        return 0;
    }

    while (!b_end) {
        cout << "-> ";
        cin >> buffer;
        my_socket_client.send_data(my_socket_client.get_local_fd(), buffer, strlen(buffer));
        if (strcmp(buffer, "q") == 0 || strcmp(buffer, "q!") == 0) {
            b_end = true;
        }
        my_socket_client.recv_data(my_socket_client.get_local_fd(), buffer, sizeof(buffer), recv_len);
    }


    NhSocket::close_socket(my_socket_client.get_local_fd());
    printf("client exit");
    return 1;
}